package shaman.storage.impl.file;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import shaman.storage.api.Filter;
import shaman.storage.impl.CompositeFilter;
import shaman.storage.impl.stream.gather.GatherInputStream;
import shaman.storage.impl.stream.gather.PartStreamFactory;
import shaman.storage.impl.stream.gather.ScatterOutputStream;

public class FileMetadataAccessor implements FileContentsAccessor {

    private final PartStreamFactory acc;
    private final Filter mainFilter;
    
    public FileMetadataAccessor(PartStreamFactory acc, List<Filter> mainFilters) {
        this.acc = acc;
        mainFilter = new CompositeFilter(mainFilters);
    }

    public InputStream fetchFileContents() throws FileNotFoundException, IOException {
        InputStream baseStream = new GatherInputStream(acc);
        InputStream filtStream = mainFilter.getInputStream(baseStream);
        return filtStream;
    }

    public OutputStream storeFileContents() throws FileNotFoundException, IOException {
        OutputStream baseStream = new ScatterOutputStream(acc);
        OutputStream filtStream = mainFilter.getOutputStream(baseStream);
        return filtStream;
    }
}
